package be.kuleuven.mume.listProxys;

import be.kuleuven.mume.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

public class LastCellLoadingProxy implements ListAdapter{
	protected static int LOADING_TYPE = 0;
	protected static int NO_DATA_TYPE = 1;
	protected static int countTypes = 2;
	protected static int LOADING_ID = R.layout.rij_laden;
	protected  int NO_DATA_ID = R.layout.rij_simpel;
	public int noDataString_ID = R.string.no_data;
	
	protected ListAdapter adapter;
	protected State state = State.Loading;
	
	protected enum State{
		Loading,NoData,Loaded
	}
	
	public LastCellLoadingProxy(ListAdapter adapter) {
		this.adapter = adapter;
	}
	
	public Object getItem(int position){
		if(position >= adapter.getCount())
			return null;
		return adapter.getItem(position);
	}
	
	public void setIsLoading(boolean isLoading){
		if(isLoading)
			this.state = State.Loading;
		else if(adapter.getCount() == 0)
			this.state = State.NoData;
		else
			this.state = State.Loaded;
	}
	
	public int getCount() {
		if(state.equals(State.Loading) | state.equals(State.NoData))
			return adapter.getCount() + 1;
		return adapter.getCount();
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if(getItemViewType(position) == LOADING_TYPE) {
			assert position >= adapter.getCount();
			return getRowView(convertView, LOADING_ID, parent);
		}
		if(getItemViewType(position) == NO_DATA_TYPE){
			assert position == 0;
			TextView view = (TextView)getRowView(convertView, NO_DATA_ID, parent);
			view.setText(noDataString_ID);
			return view;
		}
		
		assert (position < adapter.getCount());
		return adapter.getView(position, convertView, parent);
	}
	
	protected View getRowView(View convertView, int rowLayoutId, ViewGroup parent) {
		if (convertView == null){
			LayoutInflater vi = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			return vi.inflate(rowLayoutId, null);
		} else
			return convertView;
	}
	
	public long getItemId(int position) {
		if(state.equals(State.Loading) && position >= adapter.getCount())
			return 0;
		if(state.equals(NO_DATA_ID)) {
			assert position == 0;
			return 1;
		}
		return position + 2;
	}

	public int getItemViewType(int position) {
		assert (!this.state.equals(State.NoData) || position == 0);
		
		if(this.state.equals(State.Loading) && position >= adapter.getCount())
			return LOADING_TYPE;
		else if(this.state.equals(State.NoData))
			return NO_DATA_TYPE;
		
		assert (position < adapter.getCount());
		return countTypes + adapter.getItemViewType(position);
	}

	public int getViewTypeCount() {
		return countTypes + adapter.getViewTypeCount();
	}

	public boolean hasStableIds() {
		return adapter.hasStableIds();
	}

	public boolean isEmpty() {
		return adapter.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		adapter.registerDataSetObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		adapter.unregisterDataSetObserver(observer);
	}

	public boolean areAllItemsEnabled() {
		return adapter.areAllItemsEnabled();
	}

	public boolean isEnabled(int position) {
		if(position >= adapter.getCount())
			return false;
		return adapter.isEnabled(position);
	}

	public void refresh() {
		if(adapter.getCount() == 0)
			this.state = State.NoData;
		else
			this.state = State.Loaded;
	}
}
